home *** CD-ROM | disk | FTP | other *** search
/ Champak 132 (Alt) / Vol 132.iso / games / pupworld.swf / scripts / __Packages / Heroes / HeavyEllipticalCylinder.as < prev    next >
Encoding:
Text File  |  2011-06-09  |  6.6 KB  |  205 lines

  1. class Heroes.HeavyEllipticalCylinder extends Heroes.WorldObject
  2. {
  3.    var hasBeenBumped;
  4.    var xr;
  5.    var zr;
  6.    var zMax;
  7.    var zMin;
  8.    var bounds_mc;
  9.    var x;
  10.    var isRectangle;
  11.    var prevX;
  12.    var prevZ;
  13.    var z;
  14.    var collisionSounds;
  15.    var lastCollisionSoundTime;
  16.    var lastCollisionSoundNumber;
  17.    function HeavyEllipticalCylinder()
  18.    {
  19.       super();
  20.       this.hasBeenBumped = false;
  21.       if(this.xr == -1)
  22.       {
  23.          this.xr = this._width * 0.5;
  24.       }
  25.       if(this.zr == -1)
  26.       {
  27.          this.zr = this._width * 0.5;
  28.       }
  29.       this.zMax = 450;
  30.       this.zMin = -200;
  31.       if(this.bounds_mc != null)
  32.       {
  33.          this.bounds_mc._visible = false;
  34.          this.xr = this.bounds_mc._width / 2.4;
  35.          this.zr = this.bounds_mc._height / 2;
  36.       }
  37.       this.xr += Heroes.Player.boundBoxWidth / 2;
  38.       this.zr += Heroes.Player.boundBoxDepth / 2;
  39.    }
  40.    function onEnterFrame()
  41.    {
  42.       if(super.isOnScreen())
  43.       {
  44.          this._visible = true;
  45.          var _loc3_ = this.x - this._parent.player.x;
  46.          if(Math.abs(_loc3_) <= this.xr)
  47.          {
  48.             if(this.isRectangle == true)
  49.             {
  50.                this.rectangleCollisionDetection();
  51.             }
  52.             else
  53.             {
  54.                this.ellipseCollisionDetection();
  55.             }
  56.          }
  57.          super.onEnterFrame();
  58.          this.prevX = this.x;
  59.          this.prevZ = this.x;
  60.       }
  61.       else
  62.       {
  63.          this.hasBeenBumped = false;
  64.          this._visible = false;
  65.       }
  66.    }
  67.    function ellipseCollisionDetection_old()
  68.    {
  69.       var _loc3_ = this.x - this._parent.player.x;
  70.       var _loc2_ = this.z - this._parent.player.z;
  71.       var _loc12_ = _loc3_ / this.xr;
  72.       var _loc9_ = _loc2_ / this.zr;
  73.       var _loc10_ = Math.pow(_loc12_,2) + Math.pow(_loc9_,2);
  74.       if(_loc10_ <= 1)
  75.       {
  76.          this.hasBeenBumped = true;
  77.          var _loc4_ = Math.sqrt(Math.pow(_loc3_,2) + Math.pow(_loc2_,2));
  78.          this._parent.player.z -= 3 * _loc2_ / _loc4_;
  79.          this._parent.player.x -= 3 * _loc3_ / _loc4_;
  80.          var _loc11_ = this.x - this._parent.player.prevX;
  81.          var _loc8_ = this.z - this._parent.player.prevZ;
  82.          var _loc5_ = _loc11_ / this.xr;
  83.          var _loc6_ = _loc8_ / this.zr;
  84.          var _loc7_ = Math.sqrt(Math.pow(_loc5_,2) + Math.pow(_loc6_,2));
  85.          this._parent.player.z = this.z - _loc6_ * this.zr / _loc7_;
  86.          this._parent.player.x = this.x - _loc5_ * this.xr / _loc7_;
  87.       }
  88.    }
  89.    function ellipseCollisionDetection()
  90.    {
  91.       var _loc11_ = this.x - this._parent.player.x;
  92.       var _loc10_ = this.z - this._parent.player.z;
  93.       var _loc3_ = _loc11_ / this.xr;
  94.       var _loc5_ = _loc10_ / this.zr;
  95.       var _loc12_ = Math.pow(_loc5_,2) + Math.pow(_loc3_,2);
  96.       if(_loc12_ <= 1)
  97.       {
  98.          this.hasBeenBumped = true;
  99.          var _loc4_ = (this.x - this._parent.player.prevX) / this.xr;
  100.          var _loc2_ = (this.z - this._parent.player.prevZ) / this.zr;
  101.          var _loc9_ = _loc4_ * _loc3_ + _loc2_ * _loc5_;
  102.          if(_loc9_ < 0)
  103.          {
  104.             trace("Player Crossed the center");
  105.             _loc5_ = _loc2_;
  106.             _loc3_ = _loc4_;
  107.          }
  108.          var _loc6_ = _loc4_ + (_loc3_ - _loc4_) * 0.5;
  109.          var _loc8_ = _loc2_ + (_loc5_ - _loc2_) * 0.5;
  110.          var _loc7_ = Math.sqrt(Math.pow(_loc6_,2) + Math.pow(_loc8_,2));
  111.          this._parent.player.z = this.z - _loc8_ * this.zr / _loc7_;
  112.          this._parent.player.x = this.x - _loc6_ * this.xr / _loc7_;
  113.          this.playCollisionSound();
  114.       }
  115.    }
  116.    function rectangleCollisionDetection()
  117.    {
  118.       var _loc5_ = this.x - this._parent.player.x;
  119.       var _loc4_ = this.z - this._parent.player.z;
  120.       if(Math.abs(_loc5_) <= this.xr && Math.abs(_loc4_) <= this.zr)
  121.       {
  122.          var _loc7_ = this.x - this._parent.player.prevX;
  123.          var _loc6_ = this.z - this._parent.player.prevZ;
  124.          var _loc3_ = this._parent.player.x;
  125.          var _loc2_ = this._parent.player.z;
  126.          if(_loc7_ <= - this.xr)
  127.          {
  128.             _loc3_ = Math.max(this.x + this.xr + 1,this._parent.player.x);
  129.          }
  130.          if(_loc7_ >= this.xr)
  131.          {
  132.             _loc3_ = Math.min(this.x - this.xr - 1,this._parent.player.x);
  133.          }
  134.          if(_loc6_ <= - this.zr)
  135.          {
  136.             _loc2_ = Math.max(this.z + this.zr + 1,this._parent.player.z);
  137.          }
  138.          if(_loc6_ >= this.zr)
  139.          {
  140.             _loc2_ = Math.min(this.z - this.zr - 1,this._parent.player.z);
  141.          }
  142.          _loc5_ = this.x - _loc3_;
  143.          _loc4_ = this.z - _loc2_;
  144.          if(Math.abs(_loc5_) <= this.xr && Math.abs(_loc4_) <= this.zr)
  145.          {
  146.             if(Math.abs(_loc5_) <= this.xr)
  147.             {
  148.                if(_loc7_ < 0)
  149.                {
  150.                   _loc3_ = Math.max(this.x + this.xr + 1,this._parent.player.x);
  151.                }
  152.                if(_loc7_ >= 0)
  153.                {
  154.                   _loc3_ = Math.min(this.x - this.xr - 1,this._parent.player.x);
  155.                }
  156.             }
  157.             if(Math.abs(_loc4_) <= this.zr)
  158.             {
  159.                if(_loc6_ < 0)
  160.                {
  161.                   _loc2_ = Math.max(this.z + this.zr + 1,this._parent.player.z);
  162.                }
  163.                if(_loc6_ >= 0)
  164.                {
  165.                   _loc2_ = Math.min(this.z - this.zr - 1,this._parent.player.z);
  166.                }
  167.             }
  168.          }
  169.          this._parent.player.x = _loc3_;
  170.          this._parent.player.z = _loc2_;
  171.          this.playCollisionSound();
  172.       }
  173.    }
  174.    function playCollisionSound()
  175.    {
  176.       if(this.collisionSounds != null && this.collisionSounds.length > 0)
  177.       {
  178.          var _loc4_ = [6.5,4,3];
  179.          var _loc2_ = _loc4_[Math.min(this.collisionSounds.length - 1,3)] * 1000;
  180.          if(this.lastCollisionSoundTime == null)
  181.          {
  182.             this.lastCollisionSoundTime = - _loc2_;
  183.          }
  184.          var _loc3_ = getTimer() - this.lastCollisionSoundTime;
  185.          if(_loc3_ > _loc2_)
  186.          {
  187.             this.lastCollisionSoundTime = getTimer();
  188.             if(this.lastCollisionSoundNumber == null)
  189.             {
  190.                this.lastCollisionSoundNumber = 0;
  191.             }
  192.             else
  193.             {
  194.                this.lastCollisionSoundNumber = this.lastCollisionSoundNumber + 1;
  195.             }
  196.             if(this.lastCollisionSoundNumber >= this.collisionSounds.length)
  197.             {
  198.                this.lastCollisionSoundNumber = 0;
  199.             }
  200.             this.playSound(this.collisionSounds[this.lastCollisionSoundNumber]);
  201.          }
  202.       }
  203.    }
  204. }
  205.